//
//  SortViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/28.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SortViewController.h"
#import "SortTableViewCell.h"
#import "SortModel.h"
#import "SortSecondTableViewCell.h"
#import "SortDetailedViewController.h"
@interface SortViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *SortTableView;
@property (nonatomic ,strong)NSMutableDictionary *SortDic;
@property (nonatomic ,strong)NSMutableArray *SortArr;
@property (nonatomic ,strong)NSMutableArray *sortXinWenArr;
@property (nonatomic ,strong)NSMutableArray *SortGuangBoArr;
@property (nonatomic ,strong)NSMutableArray *SortXiaoYuanArr;
@end

@implementation SortViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO ;
}

-(void)netWorking
{
    [NetWorkManager GET:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=11&scale=2" isCaChe:YES resultData:^(id responseObject) {
        self.SortDic=responseObject;
        self.SortArr=[self.SortDic objectForKey:@"list"];
        self.sortXinWenArr=[NSMutableArray array];
        for (NSInteger i=0; i<self.SortArr.count; i++) {
            if (i>=5 && i<=10 ) {
                
                [self.sortXinWenArr addObject:[self.SortArr objectAtIndex:i]];
            }
        }
        self.SortGuangBoArr=[NSMutableArray array];
        for (NSInteger i=0; i<self.SortArr.count; i++) {
            if (i>10 && i<=16 ) {
                
                [self.SortGuangBoArr addObject:[self.SortArr objectAtIndex:i]];
            }
        }
        self.SortXiaoYuanArr=[NSMutableArray array];
        for (NSInteger i=0; i<self.SortArr.count; i++) {
            if (i>16 && i<=22 ) {
                
                [self.SortXiaoYuanArr addObject:[self.SortArr objectAtIndex:i]];
            }
        }
        [self.SortTableView reloadData];
    } resultRrror:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self netWorking];
    
    

    self.SortTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Height) style:UITableViewStylePlain];
    self.SortTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.SortTableView.delegate=self;
    self.SortTableView.dataSource=self;
    [self.view addSubview:self.SortTableView];
    
    
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    if(self.SortDic == nil || self.SortDic.count == 0){
       return 0;
    }
       return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
      
        SortTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"sortcell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!cell) {
            cell=[[SortTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"sortcell1"];
    }
      

            [cell.imageV1 sd_setImageWithURL:[NSURL URLWithString:[[self.SortArr firstObject] objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
            [cell.imageV2 sd_setImageWithURL:[NSURL URLWithString:[[self.SortArr objectAtIndex:1] objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
            [cell.imageV3 sd_setImageWithURL:[NSURL URLWithString:[[self.SortArr objectAtIndex:2] objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
            [cell.imageV4 sd_setImageWithURL:[NSURL URLWithString:[[self.SortArr objectAtIndex:3] objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];

            [cell.imageV5 sd_setImageWithURL:[NSURL URLWithString:[[self.SortArr objectAtIndex:4] objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];

        [cell.button1 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        cell.button1.tag=20001;
        [cell.button2 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        cell.button2.tag=20002;
        [cell.button3 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        cell.button3.tag=20003;
        [cell.button4 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        cell.button4.tag=20004;
        [cell.button5 addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        cell.button5.tag=20005;
        
        return cell;
    
    }else if (indexPath.section==1)
    {
        SortSecondTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"xinwen"];
        if (!cell) {
            cell=[[SortSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"xinwen"];
        }
              [cell.collectionView reloadData];
        cell.cellArr=self.sortXinWenArr;
  
        
        void(^block1)(void)=^(void){
            SortDetailedViewController *sort=[[SortDetailedViewController alloc]init];
            sort.str=cell.title;
            sort.num=cell.num;
            self. tabBarController. tabBar. hidden = YES ;
            [self.navigationController pushViewController:sort animated:YES];
        };
        cell.block=block1;
        

        return cell;
    }else if (indexPath.section==2){
        
        SortSecondTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"guangbo"];
        if (!cell) {
            cell=[[SortSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"guangbo"];
        }
        cell.cellArr=self.SortGuangBoArr;
           [cell.collectionView reloadData];
        
        void(^block2)(void)=^(void){
            SortDetailedViewController *sort=[[SortDetailedViewController alloc]init];
            sort.str=cell.title;
            sort.num=cell.num;
            self. tabBarController. tabBar. hidden = YES ;
            [self.navigationController pushViewController:sort animated:YES];
        };
        cell.block=block2;


        return cell;
        
    }else if (indexPath.section==3){
        
        SortSecondTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"xiaoyuan"];
        if (!cell) {
            cell=[[SortSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"xiaoyuan"];
        }
        
        
        cell.cellArr=self.SortXiaoYuanArr;
      
        void(^block3)(void)=^(void){
            SortDetailedViewController *sort=[[SortDetailedViewController alloc]init];
            sort.str=cell.title;
            sort.num=cell.num;
            self. tabBarController. tabBar. hidden = YES ;
            [self.navigationController pushViewController:sort animated:YES];
        };
        cell.block=block3;
  [cell.collectionView reloadData];
        return cell;
 
        
    }
  
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return FM_Screen_Pts_Height*210;
    }
    
        return FM_Screen_Pts_Height*140;
    
    
}
//section 分区间隔
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FM_Screen_Pts_Height*15;
}
//button点击方法
-(void)button:(UIButton *)bu
{
    SortDetailedViewController *sortDetailed=[[SortDetailedViewController alloc]init];
    
    if (bu.tag==20001) {
        sortDetailed.num =[[self.SortArr objectAtIndex:0] objectForKey:@"id"];
        sortDetailed.str =[[self.SortArr objectAtIndex:0] objectForKey:@"title"];
    }else if (bu.tag==20002){
        sortDetailed.num =[[self.SortArr objectAtIndex:1] objectForKey:@"id"];
        sortDetailed.str =[[self.SortArr objectAtIndex:1] objectForKey:@"title"];
    }else if (bu.tag==20003){
        sortDetailed.num =[[self.SortArr objectAtIndex:2] objectForKey:@"id"];
        sortDetailed.str =[[self.SortArr objectAtIndex:2] objectForKey:@"title"];
    }else if (bu.tag==20004){
        sortDetailed.num =[[self.SortArr objectAtIndex:3] objectForKey:@"id"];
        sortDetailed.str =[[self.SortArr objectAtIndex:3] objectForKey:@"title"];
    }else if (bu.tag==20005){
        sortDetailed.num =[[self.SortArr objectAtIndex:4] objectForKey:@"id"];
        sortDetailed.str =[[self.SortArr objectAtIndex:4] objectForKey:@"title"];
    }
    self. tabBarController. tabBar. hidden = YES ;
    [self.navigationController pushViewController:sortDetailed animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
